package com.example.spark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.AffairDao;
import com.example.spark.entity.Affair;
import com.example.spark.entity.dto.PaginationDTO;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.AffairService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class AffairServiceImpl extends ServiceImpl<AffairDao, Affair> implements AffairService {
    @Resource
    MinioCloudStorageService minIoService;

    private final AffairDao affairDao;

    public AffairServiceImpl(AffairDao affairDao) {
        this.affairDao = affairDao;
    }

    /**
     * 获取业务
     * * 模糊查询，并分页
     */
    @Override
    public CommonResult<IPage<Affair>> getAll(PaginationDTO<Affair> pageDto) {
        LambdaQueryWrapper<Affair> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(pageDto.getEntity().getName()),Affair::getName, pageDto.getEntity().getName());
        IPage<Affair> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        affairDao.selectPage(page,lqw);
        if (pageDto.getCurrentPage() > page.getPages()){
            pageDto.setCurrentPage((int) page.getPages());
            return getAll(pageDto);
        }
        return new CommonResult<>(200,"查询成功", page);
    }

    /**
     * 添加业务信息
     */
    @Override
    public CommonResult<Affair> saveOne(Affair affair) {
        affair.setPhoto("");
        affairDao.insert(affair);
        return new CommonResult<>(200,"添加成功",affair);
    }

    /**
     * 修改业务图片
     */

    @Override
    public CommonResult<String> updatePhoto(MultipartFile photo, Long id) {
        Affair affair = affairDao.selectById(id);
        if (affair == null) return new CommonResult<>(200,"用户不存在", "");
        try {
            String photoPath = getPhotoPath(photo, affair);
            affair.setPhoto(photoPath);
            affairDao.updateById(affair);
            return new CommonResult<>(200,"添加成功",affair.getPhoto());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取图片URL
     */

    private String getPhotoPath(MultipartFile photo, Affair affair) throws IOException {
        String photoExtension = FilenameUtils.getExtension(photo.getOriginalFilename());
        return minIoService.uploadPhotoSuffix(photo.getBytes(),photoExtension, String.valueOf(affair.getId()));
    }

    /**
     * 删除业务
     */
    @Override
    public CommonResult<Long> deleteById(Long id){
        affairDao.deleteById(id);
        return new CommonResult<>(200,"删除成功",id);
    }

    /**
     * 修改业务
     */
    @Override
    public CommonResult<Affair> updateOne(Affair affair) {
        affairDao.updateById(affair);
        return new CommonResult<>(200,"修改成功",affair);
    }
}
